<template>
  <el-container>
    <el-header>
      <div class="logo">
        <img src="../assets/img/logo.png">
        <span class="big-title">电商管理平台</span>
      </div>
      <el-button type="danger" size="small" @click="loginOut">退出登录</el-button>
    </el-header>
    <el-container>
      <el-aside :width="flag ? '64px' : '200px'">
        <el-menu
          background-color="#373d41"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse-transition="false"
          unique-opened
          :collapse="flag"
          router
        >
          <div class="toggle" @click="toggle">
            <i :class="flagIcon"></i>
          </div>
          <el-submenu :index="menu.id+''" v-for="menu in menus" :key="menu.id">
            <template slot="title">
              <i :class="icons[menu.id]"></i>
              <span>{{menu.authName}}</span>
            </template>
            <el-menu-item v-for="item in menu.children" :key="item.id" :index="`/${item.path}`">
              <i class="el-icon-menu"></i>
              <span>{{item.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menus: [],
      icons: {
        125: 'el-icon-user-solid',
        103: 'el-icon-postcard',
        101: 'el-icon-goods',
        102: 'el-icon-tickets',
        145: 'el-icon-s-marketing'
      },
      flag: false
    }
  },
  methods: {
    async getMeuns () {
      const { data } = await this.$http.get('/menus')
      if (data.meta.status === 200) {
        this.menus = data.data
      } else {
        this.$message.error('菜单获取失败')
      }
    },
    loginOut () {
      sessionStorage.clear('token')
      this.$router.push('/login')
    },
    toggle () {
      this.flag = !this.flag
    }
  },
  computed: {
    flagIcon () {
      return this.flag ? 'el-icon-caret-right' : 'el-icon-caret-left'
    }
  },
  created () {
    this.getMeuns()
  }
}
</script>

<style lang="scss" scoped>
.el-container {
  width: 100%;
  height: 100%;
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: rgb(55, 61, 65);
    height: 60px;
    .logo {
      display: flex;
      align-items: center;
      img {
        width: 40px;
        height: 40px;
      }
      .big-title {
        margin-left: 10px;
        color: #eee;
        font-size: 22px;
      }
    }
  }
  .el-container {
    .el-aside {
      background-color: #373d41;
      .el-menu {
        border: none;
        .toggle {
          background-color: #4a5064;
          text-align: center;
          padding: 4px 0;
          color: #fff;
          cursor: pointer
        }
      }
    }
  }
}
</style>
